package cool.taomu.toolkit.rpc

import com.google.auto.service.AutoService
import com.google.inject.AbstractModule
import com.google.inject.TypeLiteral
import cool.taomu.toolkit.TaomuConfigure
import cool.taomu.toolkit.platform.inter.AbsPlatform
import cool.taomu.toolkit.platform.inter.IPlatform
import cool.taomu.toolkit.rpc.thrift.entity.ThriftConfig
import java.util.List
import java.util.Map
import org.apache.commons.beanutils.BeanUtils

@AutoService(IPlatform)
class ThriftServerPlugin extends AbsPlatform {

    override note() {
        "Thrift 服务器插件"
    }

    override packages() {
    }

    override module(TaomuConfigure config) {
        new AbstractModule() {
            override configure() {
                val List<ThriftConfig> list = newArrayList();
                config.binder("taomu.rpc.server.thrift", [
                    if (it instanceof Map) {
                        var config = new ThriftConfig();
                        BeanUtils.populate(config, it);
                        list.add(config)
                    }
                ])
                bind(new TypeLiteral<List<ThriftConfig>>(){}).toInstance(list)
                //install(new ThritfTHsHaServerModule())
            }
        }
    // new ThritfTHsHaServerModule();
    // new ThritfTThreadPoolServerModule();
    }

    override start() {
        /*val server = context.injectorSupplier.get.getInstance(TaomuThriftTHsHaServer);
         * server.THsHaArgs.forEach [ k, v |
         * 	val vv = context.get(k);
         * 	if (vv instanceof TProcessor) {
         * 		context.executor.submit([
         * 			server.get(k, vv).serve
         * 		])
         * 	}
         ]*/
    }

    override init() {
    }

}
